<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Gass_auth_teachers extends CI_Controller {
  function __construct() {
    parent::__construct();
		
		$this->load->database();
		$this->load->library('session');
 		$this->load->helper('url');
 		$this->load->helper('form');

  	$this->auth = new stdClass;

		$this->load->library('flexi_auth');	
    if (! $this->flexi_auth->is_logged_in_via_password() || ! $this->flexi_auth->is_admin()) {
			$this->flexi_auth->set_error_message(urlencode('学生用户没有访问这个部分的操作权限！'), TRUE);
			$this->session->set_flashdata('message', $this->flexi_auth->get_messages());
			redirect('gass_auth');
		}

		// Note: This is only included to create base urls for purposes of this demo only and are not necessarily considered as 'Best practice'.
		$this->load->vars('base_url', $this->config->item('base_url'));
		$this->load->vars('includes_dir', $this->config->item('includes_dir'));
		$this->load->vars('current_url', $this->uri->uri_to_assoc(1));
		$this->config->load('gass_conf');
		// Define a global variable to store data that is then used by the end view page.
		$this->data = null;
		//$this->output->enable_profiler(TRUE);
	}




	###++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++###	
	// Dashboard
	###++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++###	

	/**
	 * index
	 * Forwards to the admin dashboard.
	 */ 
	function index()
    {
		$this->dashboard();
	}
 
 	/**
 	 * dashboard (Admin)
 	 * The public account dashboard page that acts as the landing page for newly logged in public users.
 	 * The dashboard provides links to some examples of the features available from the flexi auth library.  
 	 */
  function dashboard(){
    $this->data['message'] = $this->session->flashdata('message');
		$this->load->view('gass_teachers/dashboard_view', $this->data);
	}

	###++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++###	
	// 用户账户
	###++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++###	
	
 	/**
 	 * manage_user_accounts
 	 * 浏览查看所有用户账户
 	 * 允许对个别账户进行冻结或者删除操作
 	 * 还包括一个基本的搜索功能，查找制定账户 
 	 */
  function manage_user_accounts(){
		$this->load->model('gass_users_model');
		//检查用户是否拥有访问权限
		if (! $this->flexi_auth->is_privileged('user_m')){
			$this->session->set_flashdata('message', '<p class="error_msg">您没有浏览、操作系统用户的权限，请与系统管理员联系！</p>');
      redirect('gass_auth_teachers');
		}
		if ($this->input->post('search_query') || $this->input->post('select_u_group')) {
			$search_query = str_replace(' ','-',$this->input->post('search_query'));
			if(empty($search_query)) $search_query="0000";
		  redirect('gass_auth_teachers/manage_user_accounts/search_key_words/'.$search_query.'/select_u_group/'.$this->input->post('select_u_group').'/page/');
        }else if ($this->input->post('update_users')) {
			$this->gass_users_model->update_user_accounts();
		}
		$this->gass_users_model->get_user_accounts();
		
		$this->data['user_groups'] = $this->flexi_auth->get_groups()->result_array();
		$this->data['message'] = (! isset($this->data['message'])) ? $this->session->flashdata('message') : $this->data['message'];		
          // 对系统报错信息（p标签包裹的数据）进行重新处理；        
          if (!empty($this->data['message']))
          {
              $this->data['alert_message'] = $this->gass_global_model->get_alert_message($this->data['message']);   
          } 
		$this->load->view('gass_users/u_accounts_view', $this->data);		
  }
  
  
  	
 	/**
 	 * 更新用户账户的数据聚合页面
 	 *
 	 * 更新用户基本的账户信息，主要用于重新设定用户角色
 	 * 以及为分院老师角色选择相应学院
 	 */
	function update_user_account($user_id)
	{
		if (! $this->flexi_auth->is_privileged('user_m'))
		{
			$this->session->set_flashdata('message', '<p class="error_msg">您没有设置用户细节信息的权限，请与系统管理员联系！</p>');
      redirect('gass_auth_teachers');
		}

		if ($this->input->post('update_form_submit')) 
		{
			$this->load->model('gass_users_model');
			$this->gass_users_model->update_user_account($user_id);
		}
		
		$sql_where = array($this->flexi_auth->db_column('user_acc', 'id') => $user_id);
		$this->data['user'] = $this->flexi_auth->get_users_row_array(FALSE, $sql_where);
	
		//获取角色列表
		$this->data['groups'] = $this->flexi_auth->get_groups_array();
		//获取分院列表
		$this->data['colleges'] = $this->gass_global_model->get_colleges_array();
		$this->data['message'] = (! isset($this->data['message'])) ? $this->session->flashdata('message') : $this->data['message'];		
          // 对系统报错信息（p标签包裹的数据）进行重新处理；        
          if (!empty($this->data['message']))
          {
              $this->data['alert_message'] = $this->gass_global_model->get_alert_message($this->data['message']);   
          } 
		$this->load->view('gass_teachers/u_account_update_view', $this->data);
	}

  ###++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++###	
	// User Groups
	###++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++###	
	
 	/**
 	 * manage_user_groups
 	 * 浏览查看所有用户角色
 	 * 允许对个别角色进行删除操作
 	 */
  function manage_user_groups(){
		//检查用户是否拥有访问权限
		if (! $this->flexi_auth->is_privileged('user_m')){
			$this->session->set_flashdata('message', '<p class="error_msg">您没有浏览、操作系统用户的权限，请与系统管理员联系！</p>');
      redirect('gass_auth_teachers');
		}

		// If 'Manage User Group' form has been submitted and user has privileges, delete user groups.
		if ($this->input->post('delete_group')) {
			$this->load->model('gass_users_model');
			$this->gass_users_model->manage_user_groups();
		}

		// Define the group data columns to use on the view page. 
		// Note: The columns defined using the 'db_column()' functions are native table columns to the auth library. 
		// Read more on 'db_column()' functions in the quick help section near the top of this controller. 
		$sql_select = array(
			$this->flexi_auth->db_column('user_group', 'id'),
			$this->flexi_auth->db_column('user_group', 'name'),
			$this->flexi_auth->db_column('user_group', 'description'),
			$this->flexi_auth->db_column('user_group', 'admin')
		);
		$this->data['user_groups'] = $this->flexi_auth->get_groups_array($sql_select);
				
		// Set any returned status/error messages.
		$this->data['message'] = (!isset($this->data['message'])) ? $this->session->flashdata('message') : $this->data['message'];		
          // 对系统报错信息（p标签包裹的数据）进行重新处理；        
          if (!empty($this->data['message']))
          {
              $this->data['alert_message'] = $this->gass_global_model->get_alert_message($this->data['message']);   
          } 
		$this->load->view('gass_users/u_groups_view', $this->data);		
    }	
	
 	/**
 	 * insert_user_group
 	 * Insert a new user group.
 	 */
	function insert_user_group()
	{
		// Check user has privileges to insert user groups, else display a message to notify the user they do not have valid privileges.
		if (! $this->flexi_auth->is_privileged('Insert User Groups'))
		{
			$this->session->set_flashdata('message', '<p class="error_msg">You do not have privileges to insert new user groups.</p>');
			redirect('auth_admin/manage_user_groups');		
		}

		// If 'Add User Group' form has been submitted, insert the new user group.
		if ($this->input->post('insert_user_group')) 
		{
			$this->load->model('demo_auth_admin_model');
			$this->demo_auth_admin_model->insert_user_group();
		}
		
		// Set any returned status/error messages.
		$this->data['message'] = (!isset($this->data['message'])) ? $this->session->flashdata('message') : $this->data['message'];		

		$this->load->view('demo/admin_examples/user_group_insert_view', $this->data);
	}
	
 	/**
 	 * update_user_group
 	 * Update the details of a specific user group.
 	 */
	function update_user_group($group_id)
	{
		// Check user has privileges to update user groups, else display a message to notify the user they do not have valid privileges.
		if (! $this->flexi_auth->is_privileged('Update User Groups'))
		{
			$this->session->set_flashdata('message', '<p class="error_msg">You do not have privileges to update user groups.</p>');
			redirect('auth_admin/manage_user_groups');		
		}

		// If 'Update User Group' form has been submitted, update the user group details.
		if ($this->input->post('update_user_group')) 
		{
			$this->load->model('demo_auth_admin_model');
			$this->demo_auth_admin_model->update_user_group($group_id);
		}

		// Get user groups current data.
		$sql_where = array($this->flexi_auth->db_column('user_group', 'id') => $group_id);
		$this->data['group'] = $this->flexi_auth->get_groups_row_array(FALSE, $sql_where);
		
		// Set any returned status/error messages.
		$this->data['message'] = (!isset($this->data['message'])) ? $this->session->flashdata('message') : $this->data['message'];		

		$this->load->view('demo/admin_examples/user_group_update_view', $this->data);
	}

	###++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++###	
	// Privileges
	###++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++###	
	
 	/**
 	 * manage_privileges
 	 * View and manage a table of all user privileges.
 	 * This example allows user privileges to be deleted via checkoxes within the page.
 	 */
  function manage_privileges(){
	  //检查用户是否拥有访问权限
      if (! $this->flexi_auth->is_privileged('user_m'))
      {
			$this->session->set_flashdata('message', '<p class="error_msg">您没有浏览、操作系统用户的权限，请与系统管理员联系！</p>');
            redirect('gass_auth_teachers');
	  }
		
		// If 'Manage Privilege' form has been submitted and the user has privileges to delete privileges.
      if ($this->input->post('delete_privilege')) 
      {
			$this->load->model('gass_users_model');
			$this->gass_users_model->manage_privileges();
	  }

		// Define the privilege data columns to use on the view page. 
		// Note: The columns defined using the 'db_column()' functions are native table columns to the auth library. 
		// Read more on 'db_column()' functions in the quick help section near the top of this controller. 
		$sql_select = array(
			$this->flexi_auth->db_column('user_privileges', 'id'),
			$this->flexi_auth->db_column('user_privileges', 'name'),
			$this->flexi_auth->db_column('user_privileges', 'description')
		);
		$this->data['privileges'] = $this->flexi_auth->get_privileges_array($sql_select);
				
		// Set any returned status/error messages.
		$this->data['message'] = (!isset($this->data['message'])) ? $this->session->flashdata('message') : $this->data['message'];		
          // 对系统报错信息（p标签包裹的数据）进行重新处理；        
          if (!empty($this->data['message']))
          {
              $this->data['alert_message'] = $this->gass_global_model->get_alert_message($this->data['message']);   
          } 
		$this->load->view('gass_users/u_privileges_view', $this->data);
	}
	
 	/**
 	 * insert_privilege
 	 * Insert a new user privilege.
 	 */
	function insert_privilege(){
		//检查用户是否拥有访问权限
		if (! $this->flexi_auth->is_privileged('user_m')){
			$this->session->set_flashdata('message', '<p class="error_msg">您没有浏览、操作系统用户的权限，请与系统管理员联系！</p>');
      redirect('gass_auth_teachers');
		}

		// If 'Add Privilege' form has been submitted, insert the new privilege.
		if ($this->input->post('insert_privilege')) 
		{
			$this->load->model('gass_users_model');
			$this->gass_users_model->insert_privilege();
		}
		
		// Set any returned status/error messages.
		$this->data['message'] = (!isset($this->data['message'])) ? $this->session->flashdata('message') : $this->data['message'];		
          // 对系统报错信息（p标签包裹的数据）进行重新处理；        
          if (!empty($this->data['message']))
          {
              $this->data['alert_message'] = $this->gass_global_model->get_alert_message($this->data['message']);   
          } 
		$this->load->view('gass_users/u_privilege_insert_view', $this->data);
	}
	
 	/**
 	 * update_user_privileges
 	 * Update the access privileges of a specific user.
 	 */
    function update_user_privileges($user_id)
    {
    //检查用户是否拥有访问权限
      if (! $this->flexi_auth->is_privileged('user_m'))
      {
			$this->session->set_flashdata('message', '<p class="error_msg">您没有浏览、操作系统用户的权限，请与系统管理员联系！</p>');
            redirect('gass_auth_teachers');
	  }

		// If 'Update User Privilege' form has been submitted, update the user privileges.
		if ($this->input->post('update_user_privilege')) {
			$this->load->model('gass_users_model');
			$this->gass_users_model->update_user_privileges($user_id);
		}

		// Get users profile data.
		$sql_select = array(
			'upro_uacc_fk', 
			'upro_first_name', 
			'upro_last_name',
			$this->flexi_auth->db_column('user_acc', 'group_id'),
			$this->flexi_auth->db_column('user_group', 'name')
        );
		$sql_where = array($this->flexi_auth->db_column('user_acc', 'id') => $user_id);
		$this->data['user'] = $this->flexi_auth->get_users_row_array($sql_select, $sql_where);		

		// Get all privilege data. 
		$sql_select = array(
			$this->flexi_auth->db_column('user_privileges', 'id'),
			$this->flexi_auth->db_column('user_privileges', 'name'),
			$this->flexi_auth->db_column('user_privileges', 'description')
		);
		$this->data['privileges'] = $this->flexi_auth->get_privileges_array($sql_select);
		
		// Get user groups current privilege data.
		$sql_select = array($this->flexi_auth->db_column('user_privilege_groups', 'privilege_id'));
		$sql_where = array($this->flexi_auth->db_column('user_privilege_groups', 'group_id') => $this->data['user'][$this->flexi_auth->db_column('user_acc', 'group_id')]);
		$group_privileges = $this->flexi_auth->get_user_group_privileges_array($sql_select, $sql_where);
                
    $this->data['group_privileges'] = array();
    foreach($group_privileges as $privilege){
            $this->data['group_privileges'][] = $privilege[$this->flexi_auth->db_column('user_privilege_groups', 'privilege_id')];
    }
                
		// Get users current privilege data.
		$sql_select = array($this->flexi_auth->db_column('user_privilege_users', 'privilege_id'));
		$sql_where = array($this->flexi_auth->db_column('user_privilege_users', 'user_id') => $user_id);
		$user_privileges = $this->flexi_auth->get_user_privileges_array($sql_select, $sql_where);
	
		// For the purposes of the example demo view, create an array of ids for all the users assigned privileges.
		// The array can then be used within the view to check whether the user has a specific privilege, this data allows us to then format form input values accordingly. 
		$this->data['user_privileges'] = array();
		foreach($user_privileges as $privilege){
			$this->data['user_privileges'][] = $privilege[$this->flexi_auth->db_column('user_privilege_users', 'privilege_id')];
		}
	
		// Set any returned status/error messages.
		$this->data['message'] = (!isset($this->data['message'])) ? $this->session->flashdata('message') : $this->data['message'];		
          // 对系统报错信息（p标签包裹的数据）进行重新处理；        
          if (!empty($this->data['message']))
          {
              $this->data['alert_message'] = $this->gass_global_model->get_alert_message($this->data['message']);   
          } 
    // For demo purposes of demonstrate whether the current defined user privilege source is getting privilege data from either individual user 
    // privileges or user group privileges, load the settings array containing the current privilege sources. 
		$this->data['privilege_sources'] = $this->auth->auth_settings['privilege_sources'];
		$this->load->view('gass_users/u_account_privileges_update_view', $this->data);		
  }	
 	/**
 	 * update_privilege
 	 * Update the details of a specific user privilege.
 	 */
	function update_privilege($privilege_id){
		//检查用户是否拥有访问权限
		if (! $this->flexi_auth->is_privileged('user_m')){
			$this->session->set_flashdata('message', '<p class="error_msg">您没有浏览、操作系统用户的权限，请与系统管理员联系！</p>');
      redirect('gass_auth_teachers');
		}

		// If 'Update Privilege' form has been submitted, update the privilege details.
		if ($this->input->post('update_privilege')) {
			$this->load->model('gass_users_model');
			$this->gass_users_model->update_privilege($privilege_id);
		}
		
		// Get privileges current data.
		$sql_where = array($this->flexi_auth->db_column('user_privileges', 'id') => $privilege_id);
		$this->data['privilege'] = $this->flexi_auth->get_privileges_row_array(FALSE, $sql_where);
		
		// Set any returned status/error messages.
		$this->data['message'] = (!isset($this->data['message'])) ? $this->session->flashdata('message') : $this->data['message'];		

          // 对系统报错信息（p标签包裹的数据）进行重新处理；        
          if (!empty($this->data['message']))
          {
              $this->data['alert_message'] = $this->gass_global_model->get_alert_message($this->data['message']);   
          } 
		$this->load->view('gass_users/u_privilege_update_view', $this->data);
	}    
 	/**
 	 * update_group_privileges 
 	 * Update the access privileges of a specific user group.
 	 */
    function update_group_privileges($group_id)
    {
		//检查用户是否拥有访问权限
        if (! $this->flexi_auth->is_privileged('user_m'))
        {
			$this->session->set_flashdata('message', '<p class="error_msg">您没有浏览、操作系统用户的权限，请与系统管理员联系！</p>');
            redirect('gass_auth_teachers');
		}

		// If 'Update Group Privilege' form has been submitted, update the privileges of the user group.
        if ($this->input->post('update_group_privilege')) 
        {
			$this->load->model('gass_users_model');
			$this->gass_users_model->update_group_privileges($group_id);
		}
		
		// Get data for the current user group.
		$sql_where = array($this->flexi_auth->db_column('user_group', 'id') => $group_id);
		$this->data['group'] = $this->flexi_auth->get_groups_row_array(FALSE, $sql_where);
                
		// Get all privilege data. 
		$sql_select = array(
			$this->flexi_auth->db_column('user_privileges', 'id'),
			$this->flexi_auth->db_column('user_privileges', 'name'),
			$this->flexi_auth->db_column('user_privileges', 'description')
		);
		$this->data['privileges'] = $this->flexi_auth->get_privileges_array($sql_select);
		
		// Get data for the current privilege group.
		$sql_select = array($this->flexi_auth->db_column('user_privilege_groups', 'privilege_id'));
		$sql_where = array($this->flexi_auth->db_column('user_privilege_groups', 'group_id') => $group_id);
		$group_privileges = $this->flexi_auth->get_user_group_privileges_array($sql_select, $sql_where);
                
		// For the purposes of the example demo view, create an array of ids for all the privileges that have been assigned to a privilege group.
		// The array can then be used within the view to check whether the group has a specific privilege, this data allows us to then format form input values accordingly. 
		$this->data['group_privileges'] = array();
		foreach($group_privileges as $privilege)
		{
			$this->data['group_privileges'][] = $privilege[$this->flexi_auth->db_column('user_privilege_groups', 'privilege_id')];
		}
	
		// Set any returned status/error messages.
		$this->data['message'] = (!isset($this->data['message'])) ? $this->session->flashdata('message') : $this->data['message'];	
    $this->data['privilege_sources'] = $this->auth->auth_settings['privilege_sources'];
                
          // 对系统报错信息（p标签包裹的数据）进行重新处理；        
          if (!empty($this->data['message']))
          {
              $this->data['alert_message'] = $this->gass_global_model->get_alert_message($this->data['message']);   
          } 
		$this->load->view('gass_users/u_group_privileges_update_view', $this->data);		
  }

}

/* End of file gass_auth_teachers.php */
/* Location: ./application/controllers/gass_auth_teachers.php */
